package myweka;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.DriverManager;
import java.util.Properties;
import java.util.Vector;

import myio.FileIO;


import weka.core.*;

public class ArffToTETdata {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Instances arffdata = null;
		try{
		    FileReader reader = new FileReader(args[0]); 
		    arffdata = new Instances(reader);
		}
		catch (IOException e) {System.out.println(e);}
		
		/* Initialize database */
		 java.sql.PreparedStatement pst;
		 java.sql.Connection myconnection = null;
		 String commandstring;
		 String tablename;
		 
		 String configfiletypestring = "";
		 String configfilermodestring = "";
		 
		 try{
			 Class.forName("com.mysql.jdbc.Driver");
			 Properties props = new Properties();
			 props.setProperty("user","mysql");

			 myconnection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root");

			 pst = myconnection.prepareStatement("CREATE DATABASE " + args[1]);
			 pst.execute();

			 pst  = myconnection.prepareStatement("USE " + args[1]);
			 pst.execute();
		 }
		 catch(java.sql.SQLException e){System.out.println(e);}
		 catch(java.lang.ClassNotFoundException e){System.out.println(e);}
		 
		 /*********************
		  * Initialize Tables *
		  *********************/
		 
		 /* 'objects' table */
		 try{
			 pst = myconnection.prepareStatement("CREATE TABLE objects (arg1 INT)");
			 pst.execute();
		 }
		 catch(java.sql.SQLException e){System.out.println(e);}

		 /* Iterate over attributes */

		 Attribute nextattribute;

		 try{
			 for (int i=0;i<arffdata.numAttributes();i++){
				 nextattribute = arffdata.attribute(i);
				 if (nextattribute.type() != Attribute.NOMINAL)
					 throw new java.lang.RuntimeException(nextattribute.name() + " is not nominal!");
				 
				 /* Iterate over values of attribute */
				 
				 String nextval;
				 for (int j=0; j<nextattribute.numValues(); j++ ){
					 nextval = nextattribute.value(j);
					 
					 /* create table for this attribute value */
					 tablename = nextattribute.name() + "_" + nextval;
					 pst = myconnection.prepareStatement("CREATE TABLE " + tablename + "(arg1 INT)");
					 pst.execute();
					 
					 /* Extend configfilestrings */
					 configfiletypestring = configfiletypestring + "type(" + tablename + "(objects))" + '\n';
					 configfilermodestring = configfilermodestring + "rmode(" + tablename + "(+-))" + '\n';
 				 }
			 }
		 }
		 catch(java.sql.SQLException e){System.out.println(e);}
		 catch(java.lang.RuntimeException e){System.out.println(e);}
		 
		 /* Write configfile */
		 
		 
		 String configpath="/home/jaeger/TET/Examples/learning/" + args[1]   ;
		 (new File(configpath)).mkdirs();
		 File cffile = new File(configpath +"/configfile");
		 
		 try{
			 BufferedWriter configwriter = FileIO.openOutputFile(cffile);
			 configwriter.write(configfiletypestring + '\n');
			 configwriter.write(configfilermodestring);		 
			 configwriter.close();
		 }
		 catch (IOException e){System.out.println(e);}

		 /*********************
		  *   Fill Tables     *
		  *********************/

		 /* Fill objects table */
		 
		 try{
			 commandstring = "INSERT INTO objects VALUES ";
			 for (int h=0;h<arffdata.numInstances()-1;h++){
				 commandstring = commandstring + "(" + h + "),";
			 }
			 int lastinst = arffdata.numInstances()-1;
			 commandstring = commandstring + "(" + lastinst +")";
			 pst = myconnection.prepareStatement(commandstring);
			 pst.execute();
		 }
		 catch(java.sql.SQLException e){System.out.println(e);}
		 /* Iterate over instances */

		 Instance nextinstance;

		 try{
			 for (int h=0;h<arffdata.numInstances();h++){
				 nextinstance = arffdata.instance(h);

				 /* Iterate over attributes */
				 String nextval;

				 for (int i=0;i<arffdata.numAttributes();i++){
					 nextval = nextinstance.toString(i);
					 tablename = arffdata.attribute(i).name() + "_" + nextval;
					 pst = myconnection.prepareStatement("INSERT INTO " + tablename + " VALUES ("+ h +")");
					 pst.execute();
				 }
			 }
		 }
		 catch(java.sql.SQLException e){System.out.println(e);}
	}
}
